/*
 * $$Id: Configurator.java,v 1.16 2004/05/30 20:03:57 bel70 Exp $$
 *
 * ***** BEGIN LICENSE BLOCK *****
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License
 * at http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and
 * limitations under the License.
 *
 * The Original Code is jforum forum code.
 *
 * The Initial Developer of the Original Code is the JResearch, Org.
 * Portions created by the Initial Developer are Copyright (C) 2004
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *              Dmitry Belov <bel@jresearch.org>
 *
 * ***** END LICENSE BLOCK ***** */
/*
 * Created on Oct 29, 2003
 *
  */
package com.jred.jforum.configuration;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jred.jforum.exception.ConfiguratorException;
import com.jred.jforum.service.ForumService;

import java.io.IOException;
import java.io.InputStream;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.servlet.ServletContext;

import javax.sql.DataSource;


/**
 * DOCUMENT ME!
 *
 * @author dbelov
 */
public class Configurator {
    private static Configurator ourInstance;
    private static Log log = LogFactory.getLog(Configurator.class);
    private Properties props;
    
    protected static WebApplicationContext ctx;
    
    private Configurator() {
        this.props = new Properties();
    }

    /**
     * DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     */
    public synchronized static Configurator getInstance() {
        if (ourInstance == null) {
            ourInstance = new Configurator();
        }

        return ourInstance;
    }

    /**
     * DOCUMENT ME!
     *
     * @param key DOCUMENT ME!
     *
     * @return DOCUMENT ME!
     *
     * @throws ConfiguratorException DOCUMENT ME!
     */
    public String get(String key) throws ConfiguratorException {
        if (this.props.getProperty(key) == null) {
            throw new ConfiguratorException("Parameter by key=" + key +
                " not found");
        }

        return this.props.getProperty(key);
    }

    /**
     * DOCUMENT ME!
     *
     * @param servletContext DOCUMENT ME!
     *
     * @throws SQLException DOCUMENT ME!
     * @throws IOException DOCUMENT ME!
     */
    public void load(ServletContext servletContext)
        throws SQLException, IOException {
        if (log.isInfoEnabled()) {
            log.info("try to load app.properties");
        }

        InputStream is = servletContext.getResourceAsStream(
                "/WEB-INF/classes/com/jred/jforum/resources/app.properties");

        try {
            this.props.load(is);
            is.close();
        } catch (IOException e1) {
            if (log.isErrorEnabled()) {
                log.error("Loading of jforum's configuration failed " +
                    e1.getMessage());
            }
            throw e1;
        }

        if (log.isInfoEnabled()) {
            log.info("try to load configuration from Data Base");
        }
		ctx = WebApplicationContextUtils
				.getWebApplicationContext(servletContext);
        ForumService forumService = (ForumService) ctx.getBean("forumService");
        
        Map map = forumService.getSystemConstants();
        for(Iterator it = map.entrySet().iterator(); it.hasNext(); ){
        	Map.Entry e = (Map.Entry)it.next();
        	this.props.put(e.getKey(), e.getValue());
        }             
            
    }

    /**
     * DOCUMENT ME!
     *
     * @param servletContext DOCUMENT ME!
     *
     * @throws SQLException DOCUMENT ME!
     * @throws IOException DOCUMENT ME!
     */
    public void reload(ServletContext servletContext)
        throws SQLException, IOException {
        this.props = new Properties();
        load(servletContext);
    }

}
